package com.lanou3g.parsinglist;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RequestQueue requestQueue;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/100?client_os=android&client_version=2.1.0.1&bid=800&m_ch=004&city=0411&sn=ade627ae9c8b3165f8beab83650296fd&content_cnt=20&recoid=2987975311729206682&ftime=1452049562236&method=new&count=20";
    private MainAdapter adapter;
    private ListView listView;
    private List<Myarticle> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.main_listview);
        //TODO
        //adapter=new MainAdapter();
        //listView.setAdapter(adapter);
        Data();
    }

    public void Data() {
        requestQueue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("MainActivity", response);
                data = new ArrayList<>();
                Gson gson = new Gson();
                ResponseEty responseEty = gson.fromJson(response, ResponseEty.class);
                for (int i = 0; i < responseEty.getData().getArticle().size(); i++) {
                    Myarticle myarticle = new Myarticle();
                    myarticle.setTitle(responseEty.getData().getArticle().get(i).getTitle());
                    data.add(myarticle);
                    adapter = new MainAdapter(data, MainActivity.this);
                    listView.setAdapter(adapter);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);
    }
}
